package com.qzsoft.zhjd.machine.modules.machinerepair.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qzsoft.jeemis.common.utils.Result;
import com.qzsoft.jeemis.common.validator.ValidatorUtils;
import com.qzsoft.jeemis.common.validator.group.AddGroup;
import com.qzsoft.jeemis.common.validator.group.UpdateGroup;
import com.qzsoft.zhjd.common.enums.BizCode;
import com.qzsoft.zhjd.common.utils.ResultUtils;
import com.qzsoft.zhjd.machine.modules.machinerepair.dto.MachineRepairDto;
import com.qzsoft.zhjd.machine.modules.machinerepair.entity.MachineRepairEntity;
import com.qzsoft.zhjd.machine.modules.machinerepair.service.MachineRepairService;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @author HuXiuMing
 * @date 2021/10/21 16:21
 */

@RestController
@RequestMapping("/api/machineRepair")
public class MachineRepairController {

    @Autowired
    private MachineRepairService machineRepairService;

    /**
     * @Title page
     * @Description 获取维修分页列表
     * @param params 查询条件
     * @return 维修记录分页列表
     */
    @RequiresPermissions("api:machinerepair:list")
    @GetMapping("/page")
    public Result<IPage<MachineRepairDto>> page(@RequestParam Map<String, Object> params) {
        IPage<MachineRepairDto> page = machineRepairService.pageDto(params);
        return ResultUtils.ok(page);
    }

    /**
     * @Title page
     * @Description 获取维修分页列表
     * @param params 查询条件
     * @return 维修记录分页列表
     */
    @GetMapping("/page/{machieId}")
    public Result<IPage<MachineRepairEntity>> page(@PathVariable("machieId") String machieId,
            @RequestParam Map<String, Object> params) {
        if (null == params) {
            params = new HashMap<String, Object>();
        }
        params.put("machieId", machieId);
        IPage<MachineRepairEntity> page = machineRepairService.page(params);
        return ResultUtils.ok(page);
    }

    /**
     * @Title find
     * @Description 查询维修信息
     * @param id 维修主键
     * @return 查询结果
     */
    @RequiresPermissions("api:machinerepair:edit")
    @GetMapping("/find/{id}")
    public Result<MachineRepairEntity> find(@PathVariable("id") String id) {
        MachineRepairEntity entity = machineRepairService.getById(id);
        if (null == entity) {
            return ResultUtils.error(BizCode.DATA_NOT_FOUND);
        }
        return ResultUtils.ok(entity);
    }

    /**
     * @Title save
     * @Description 添加维修信息
     * @param entity 维修信息
     * @return 添加结果
     */
    @RequiresPermissions("api:machinerepair:save")
    @PostMapping("/save")
    public Result<String> save(@RequestBody MachineRepairEntity entity) {
        ValidatorUtils.validateEntity(entity, AddGroup.class);
        machineRepairService.save(entity);
        return ResultUtils.ok();
    }

    /**
     * @Title update
     * @Description 修改维修信息
     * @param entity 维修信息
     * @return 修改结果
     */
    @PostMapping("/update")
    public Result<String> update(@RequestBody MachineRepairEntity entity) {
        ValidatorUtils.validateEntity(entity, UpdateGroup.class);
        machineRepairService.updateById(entity);
        return ResultUtils.ok();
    }

    /**
     * @Title delete
     * @Description 删除维修信息
     * @param id 信息主键
     * @return 删除结果
     */
    @RequiresPermissions("api:machinerepair:remove")
    @PostMapping("/delete/{id}")
    public Result<String> delete(@PathVariable("id") String id) {
        machineRepairService.removeById(id);
        return ResultUtils.ok();
    }

}
